package com.itlong.cloud.commons.service.impl;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.VO.property.PropertyVisitorInfoByPushVO;
import com.itlong.cloud.commons.dao.IVisitorPushMessageDao;
import com.itlong.cloud.commons.service.IVisitorPushMessageService;
import com.itlong.cloud.push.message.GatewayService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 * App用户添加访客后主动向前端推送消息
 * </desc>
 *
 * @createDate 2018/07/13
 */
@Service
public class VisitorPushMessageServiceImpl implements IVisitorPushMessageService {

    private static final Logger LOG = LoggerFactory.getLogger(VisitorPushMessageServiceImpl.class);
    @Autowired
    IVisitorPushMessageDao iVisitorPushMessageDao;

    /**
     * <desc>
     * App用户添加访客后主动向前端推送消息
     * </desc>
     *
     * @return
     * @author JuGuang.S
     * @createDate 2018/07/13
     */
    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    public void visitorPushMessage() throws Exception {
        Map<String, Channel> map = GatewayService.getChannels();
        Iterator<String> it = map.keySet().iterator();
        List<String> passId = new ArrayList<>();
        while (it.hasNext()) {
            String key = it.next();
            Channel obj = map.get(key);

            List<PropertyVisitorInfoByPushVO> infoByPushVOSList = iVisitorPushMessageDao.getVisitorInfoByPush();
            for (PropertyVisitorInfoByPushVO infoVO : infoByPushVOSList) {
                if (infoVO.getSource() == 1 || infoVO.getSource() == 4) {
                    String adressName = iVisitorPushMessageDao.getAdressByRoomId(infoVO.getRoomId());
                    infoVO.setAddress(adressName);
                    if (StringUtils.isNotEmpty(infoVO.getIntervieweePhone()) && StringUtils.isNotEmpty(infoVO.getCheckOperator())) {
                        infoVO.setOperator(iVisitorPushMessageDao.getUserNameByUserId(infoVO.getCheckOperator()));
                    }
                }

                if (infoVO.getSource() == 2) {
                    String adressNameStr = iVisitorPushMessageDao.getAdressByUnitId(infoVO.getPassId(), infoVO.getUnitId());
                    infoVO.setAddress(adressNameStr);
                }
            }

            if (!obj.isOpen() || !obj.isConnected()) {
                GatewayService.removeGatewayChannel(key);
            }

            if (infoByPushVOSList != null && infoByPushVOSList.size() > 0) {
                String strUTF8 = URLDecoder.decode(JSON.toJSONString(infoByPushVOSList), "UTF-8");
                System.out.println("strUTF8:" + strUTF8);
                if (obj.isOpen()) {
                    obj.write(new TextWebSocketFrame(strUTF8));

                    for (PropertyVisitorInfoByPushVO pushVO : infoByPushVOSList) {
                        passId.add(pushVO.getPassId());
                    }

                } else {
                    GatewayService.removeGatewayChannel(key);
                }

            }
        }
        if (passId.size() > 0) {
            iVisitorPushMessageDao.updatePushStatus(passId.toArray(new String[passId.size()]));
        }
    }

}
